package com.example.tool.activtiy;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.tool.R;
import com.example.tool.activtiy.base.BaseActivity;
import com.example.tool.adapter.TabPagerAdapter;
import com.example.tool.fragment.SignsCountFragment;
import com.example.tool.fragment.SleepCountFragment;
import com.example.tool.fragment.SportCountFragment;
import com.example.tool.fragment.TotalCountFragment;
import com.example.tool.listener.AGException;
import com.example.tool.listener.BaseCallback;
import com.example.tool.mvp.CommonPresent;
import com.example.tool.widget.keep.indicator.ColorTransitionPagerTitleView;
import com.example.tool.widget.keep.indicator.CommonNavigator;
import com.example.tool.widget.keep.indicator.CommonNavigatorAdapter;
import com.example.tool.widget.keep.indicator.IPagerIndicator;
import com.example.tool.widget.keep.indicator.IPagerTitleView;
import com.example.tool.widget.keep.indicator.LinePagerIndicator;
import com.example.tool.widget.keep.indicator.MagicIndicator;
import com.example.tool.widget.keep.indicator.SimplePagerTitleView;
import com.example.tool.widget.keep.indicator.ViewPagerHelper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;

/**
 * 描述:仿照keep的滑动ui
 *
 * @author: liangkun
 * @date: 2019/10/8 14:26
 */
public class CopyKeepActivity extends BaseActivity<CommonPresent> {
    @Override
    protected int getLayoutId() {
        return R.layout.activity_health_behavior;
    }


    @Override
    protected CommonPresent createPresenter() {
        return null;
    }

    @BindView(R.id.tv_title)
    protected TextView tvTitle;
    @BindView(R.id.magic_indicator)
    protected MagicIndicator magicIndicator;
    @BindView(R.id.view_pager)
    protected ViewPager viewPager;
    private String[] tabChanel; //指示器个数
    private List<Fragment> mDataList = new ArrayList<>(); //个数
    private TabPagerAdapter tabPagerAdapter; //指示器适配器
    private List<String> mSrcList; //文字数组
    private TotalCountFragment oneFragment;
    private SportCountFragment twoFragment;
    private SleepCountFragment threeFragment;
    private SignsCountFragment fourFragment;


    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {
//        showLoadingDialog();
//        presenter.getHeathIndex();
        tabChanel = getResources().getStringArray(R.array.heath_count_array);
        mSrcList = Arrays.asList(tabChanel);
        initMagicIndicator();

    }

    @Override
    protected void initView() {
        tvTitle.setText("仿keep滑动");
//        //加载弹窗注册
//        EventBus.getDefault().register(this);
//        titleView.setTitle(getString(R.string.behavior_index));
//        titleView.setTitleColor(R.color.white);
//        titleView.setBackground(R.color.color_20253f);
//        titleView.setSpaceLineShow(false);
//        titleView.setBackDrawble(R.mipmap.ic_back_white);
        initFragments();
    }

    /**
     * 初始化fragment
     */
    private void initFragments() {

        oneFragment = new TotalCountFragment();
        twoFragment = new SportCountFragment();
        threeFragment = new SleepCountFragment();
        fourFragment = new SignsCountFragment();
        mDataList.add(oneFragment);
        mDataList.add(twoFragment);
        mDataList.add(threeFragment);
        mDataList.add(fourFragment);
        viewPager.setOffscreenPageLimit(4);
        tabPagerAdapter = new TabPagerAdapter(getSupportFragmentManager(), mDataList);
        viewPager.setAdapter(tabPagerAdapter);
    }

    /**
     * 初始化指示器
     */
    private void initMagicIndicator() {
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mSrcList.get(index));
                simplePagerTitleView.setTextSize(14);
                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.color_63677a));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.color_fecb00));
                simplePagerTitleView.setOnClickListener(v -> {

                    viewPager.setCurrentItem(index);
                    resertBar(index);
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.color_fecb00));
                return indicator;
            }
        });
        magicIndicator.setNavigator(commonNavigator);
        LinearLayout titleContainer = commonNavigator.getTitleContainer(); // must after setNavigator
        titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_END);
//        titleContainer.setDividerPadding((int) getResources().getDimension(R.dimen.sw_dp_8));
        ViewPagerHelper.bind(magicIndicator, viewPager, new BaseCallback() {
            @Override
            public void success(Object result) {
                int postion = (int) result;
                resertBar(postion);
            }

            @Override
            public void error(AGException e) {

            }
        });


    }

    /**
     * 还原某个位置的bar
     *
     * @param postion fragment
     */
    private void resertBar(int postion) {
        switch (postion) {
            case 0:
                oneFragment.setBarPoint();
                break;
            case 1:
                twoFragment.setBarPoint();
                break;
            case 2:
                threeFragment.setBarPoint();
                break;
            case 3:
                fourFragment.setBarPoint();
                break;
        }
    }

//
//    @Override
//    public void getHeathIndexVo(List<HealthIndexDomain> indexVos) {
//
//    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
}
